/*
 * Copyright 2011 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.dnf;

import java.io.Serializable;

/**
 * Represents Literals in a DNF, i.e. positive or negative Atoms (propositional variables).
 * 
 * A Literal consists of a identifier (String) and its polarity (positive or negative occurrence).
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 */
public class Literal implements Serializable
{
	private static final long serialVersionUID = 8068587730808041886L;
	
	/*
	 * Constant that will be added to the hashCode if the occurrence is negative.
	 */
	private static final int HASH_CONSTANT = 2500;
	
	/*
	 * Identifier of this Literal.
	 */
	private String identifier;
	/*
	 * Polarity of this Literal.
	 */
	private boolean isPositive;
	
	/**
	 * Creates a new Literal with given identifier and polarity.
	 * 
	 * @param identifier The name of the propositional variable.
	 * @param isPositive The polarity of this Literal, i.e. if isPositive is {@code true} the
	 * occurrence is positive and otherwise negative.
	 */
	public Literal(String identifier, boolean isPositive)
	{
		this.identifier = identifier;
		this.isPositive = isPositive;
	}
	
	/**
	 * Creates a new Literal with given identifier and positive polarity.
	 * 
	 * @param identifier The name of the propositional variable.
	 */
	public Literal(String identifier)
	{
		this(identifier, true);
	}

	/**
	 * Returns the identifier of this Literal, i.e. the name of the propositional variable.
	 * 
	 * @return name of the propositional variable.
	 */
	public String getIdentifier()
	{
		return identifier;
	}

	/**
	 * Returns the polarity of this Literal, i.e. returns {@code true} if this Literal represents a positive Atom and {@code false} otherwise.
	 * 
	 * @return {@code true} if the variable occurs positive and {@code false} otherwise.
	 */
	public boolean isPositive()
	{
		return isPositive;
	}
	
	/**
	 * Returns a String representation of this Literal.
	 * 
	 * The string representation consists of the identifier if the occurrence of the variable is positive
	 * and negativeChar + identifier if the occurrence is negative.
	 * 
	 * @param negativeChar The character that is used to indicate a negative occurrence of variable.
	 * 
	 * @return this.identifier if the occurrence is positive and negativeChar + this.identifier otherwise.
	 */
	public String toString(char negativeChar)
	{
		return isPositive() ? identifier : negativeChar + identifier;
	}
	
	/**
	 * Returns a String representation of this Literal.
	 * 
	 * Returns the same as {@link #toString(char)} using '~' as negativeChar.
	 * @see #toString(char)
	 */
	@Override
	public String toString()
	{
		return toString('~');
	}
	
	/**
	 * Returns an hash code for this Literal, satisfies the general contract for hashCode methods.
	 * 
	 * @return this.identifier.hashCode() + value depending on whether whether the occurrence is positive or negative.
	 */
	@Override
	public int hashCode()
	{
		return identifier.hashCode() + (isPositive() ? 0 : 1) * HASH_CONSTANT;
	}
	
	/**
	 * Returns true if other is also a Literal and the identifier and polarity of other are equal
	 * to the identifier and polarity of this Literal.
	 */
	@Override
	public boolean equals(Object other)
	{
		if(other == null)
			return false;
		if(!(other instanceof Literal))
			return false;
		Literal otherL = (Literal)other;
		return identifier.equals(otherL.getIdentifier()) && (isPositive == otherL.isPositive()); 
	}
	
}
